package com.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.pojo.User;
import com.service.UserService;

@Controller
@RequestMapping("/USER")
public class UserController {
	@Autowired
	UserService service;

	@RequestMapping("/LOGIN")
	public String userLogin(@RequestParam("username") String username,
			@RequestParam("password") String password, Model model,
			HttpServletRequest request) throws Exception {
		User user = new User();
		user.setUsername(username);
		user.setPassword(password);
		user = service.verify(user);
		if (user != null) {
			request.getSession().setAttribute("user", user);
			return "redirect:/ITEM/LIST?offset=0&limit=10";
		} else {
			return "redirect:/jsp/userlogin/failure.jsp";
		}
	}
	
	@RequestMapping("/LOGOUT")
	public String userLogout(HttpServletRequest request) throws Exception{
		request.getSession().removeAttribute("user");
		return "redirect:/";
	}
}
